<template>
  <x-dialog :title="title" :show.sync="visible" v-if="visible" :before-close="handleClose" width="40%">
    <el-form :model="params" :rules="rules" ref="params" class="demo-form-inline" label-position="top">
      <el-row :gutter="36">
        <el-col :span="12" v-if="params.parentId + '' !== '0'">
          <el-form-item :label="$t('resourceAdmin.area.preArea')" prop="parentId">
            <x-select-tree
              v-model="params.parent"
              :is-lazy="true"
              :nodeKey="'dictCode'"
              :request-url="'/upms/area/tree'"
              :defaultProps="defaultAreaProps"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('resourceAdmin.area.name')" prop="areaName">
            <el-input
              v-model="params.areaName"
              :placeholder="$t('resourceAdmin.area.placeHolderInput')"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('resourceAdmin.area.regionLeval')" prop="areaLevel">
            <x-select
              :placeholder="$t('resourceAdmin.area.placeHolderSelect')"
              v-model="params.areaLevel"
              :options="dictOptions['upms_area.area_level']"
            />
          </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
          <el-form-item :label="$t('resourceAdmin.area.areaCode')" prop="id">
            <el-input
              v-model="params.id"
              :placeholder="$t('resourceAdmin.area.placeHolderInput')"
              :disabled="params.hasOwnProperty('tenantId')"
              clearable
            ></el-input>
          </el-form-item>
        </el-col> -->

        <el-col :span="12">
          <el-form-item :label="$t('resourceAdmin.area.areaType')" prop="areaType">
            <x-radio-group :list-data="dictOptions['upms_area.area_type']" v-model="params.areaType"></x-radio-group>
          </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
          <el-form-item :label="$t('resourceAdmin.area.isHot')" prop="hotArea">
            <x-radio-group :list-data="dictOptions['upms_area.area_level']" v-model="params.hotArea"></x-radio-group>
          </el-form-item>
        </el-col> -->
        <el-col :span="12">
          <el-form-item :label="$t('resourceAdmin.area.sort')" prop="sort">
            <el-input-number v-model="params.sort" controls-position="right" :min="1"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('resourceAdmin.area.state')" prop="state">
            <x-radio-group :list-data="dictOptions['system.state']" v-model="params.state"></x-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer">
      <el-button @click="handleClose"> {{ $t('common.cancel') }} </el-button>
      <el-button
        type="primary"
        @click="
          loading = true
          handleSubmit()
        "
        :loading="loading"
      >
        {{ $t('common.save') }}
      </el-button>
      <!-- <el-button v-if="params?.id" type="danger" @click="handleDeleteArea">
        {{ $t('common.delete') }}
      </el-button> -->
    </span>
  </x-dialog>
</template>
<script>
  import { cloneDeep, omit, throttle } from '@package/utils'
  import { apiPutUpdateArea, apiPostAddArea, apiGetAreaInfo } from '../api/area.js'
  import { EnumApiCode } from '../../../../../../globalEnum'

  export default {
    props: {
      dictOptions: {
        type: Object
      },
      areaTreeOptions: {
        type: Array
      },
      defaultAreaProps: {
        type: Object
      }
    },
    data() {
      return {
        visible: false,
        loading: false,
        title: this.$t('resourceAdmin.area.add'),
        params: {
          parent: {
            areaName: '',
            id: ''
          },
          parentId: '',
          areaName: '',
          areaRegion: '',
          areaLevel: '',
          id: '',
          areaType: '1',
          // amapCenter: '',
          // hotArea: '1',
          sort: '1',
          state: '1'
        },
        isParent: true,
        rules: {
          parentId: [
            { required: true, message: this.$t('resourceAdmin.area.placeHolderSelect'), trigger: ['blur', 'change'] }
          ],
          areaName: [
            { required: true, message: this.$t('resourceAdmin.area.placeHolderInput'), trigger: 'blur' },
            { min: 1, max: 100, message: this.$t('resourceAdmin.area.inputRulesProm'), trigger: 'blur' }
          ],
          areaType: [{ required: true, message: this.$t('resourceAdmin.area.placeHolderSelect'), trigger: 'blur' }],
          id: [{ required: true, message: this.$t('resourceAdmin.area.placeHolderInput'), trigger: 'blur' }],
          areaLevel: [{ required: true, message: this.$t('resourceAdmin.area.placeHolderSelect'), trigger: 'blur' }]
        }
      }
    },
    methods: {
      handleOpen(info) {
        this.visible = true
        if (info?.id) {
          this.title = this.$t('common.edit')
          apiGetAreaInfo(info.id).then(({ data }) => {
            this.$nextTick(() => {
              this.params = Object.assign(this.params, cloneDeep(data))
              this.params.state += ''
              this.params.areaLevel += ''
              this.params.areaType += ''
              this.params.parent = {
                areaName: this.params.parentName,
                id: this.params.parentId
              }
            })
          })
          return
        }
        this.params.parentId = info?.parentId ?? '0'
        this.params.parent = {
          areaName: info.parentName,
          id: this.params.parentId
        }
        this.params.areaLevel = `${info.parentLevel + 1}`
        this.title = this.$t('resourceAdmin.area.add')
      },
      handleSubmit: throttle(function () {
        this.loading = true
        this.$refs.params.validate(async (valid) => {
          if (valid) {
            const param = {
              parentId: this.params.parent.id,
              ...omit(this.params, 'parent')
            }
            try {
              const { code, msg } = this.params.hasOwnProperty('tenantId')
                ? await apiPutUpdateArea(param)
                : await apiPostAddArea(param)
              this.loading = false
              if (code === EnumApiCode.Success) {
                this.$message.success(
                  this.params.hasOwnProperty('tenantId') ? this.$t('common.editSuccess') : this.$t('common.newSuccess')
                )
                this.handleClose()
                this.$emit('handleSuccess', param.id)
              } else {
                this.$message.error(msg)
              }
            } finally {
              this.loading = false
            }
          } else {
            this.loading = false
            return false
          }
        })
      }, 1000),
      handleClose() {
        this.$refs.params.resetFields()
        this.params = omit(this.params, ['tenantId'])
        this.loading = false
        this.visible = false
      },
      handleDeleteArea() {
        this.$emit('on-delete', this.params.id)
        this.handleClose()
      }
    }
  }
</script>
<style lang="scss" scoped>
  ::v-deep.el-input.is-disabled .el-input__inner,
  ::v-deep.el-textarea.is-disabled .el-textarea__inner {
    cursor: auto;
    color: #606266;
  }
</style>
